<?php
/**
 * Created by PhpStorm.
 * User: chenzhi
 * Date: 16/5/24
 * Time: 下午7:59
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Groupm extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }
    //获取每个群主的最大群员数
    public function top_group_list()
    {
        $this->db->select('gauid,(select count(gid) from ry_group_members where gid=a.gid) as count ');
        $data = $this->db->get("ry_group_info as a")->result_array();

        $return = [];
        foreach($data as $val)
        {
            if(($val['count'] > 0) && (!isset($return[$val['gauid']]) || ($return[$val['gauid']] < $val['count'])))
            {
                $return[$val['gauid']] = $val['count'];
            }
        }

        return $return;

    }

    /**
     * 给群主加积分
     * @param $gauid  群主id
     * @param $member_count  群员人数
     *
     *  
     */
    public function add_js_for_gauid($gauid,$member_count)
    {
        if(empty($gauid))
        {
            return array("code"=>-12,"message"=>"参数错误");
        }
        $this->load->model("newjfpointsm");

        $stage = date("Y-m").'月群主加积分';
        $desc = date("Y-m").'群主加积分';
        $type = '996';
        $one_time_jfpoints = 0;

        if($member_count >= 50 and $member_count < 100)
        {
            $one_time_jfpoints = 5;
        }
        elseif($member_count >= 100 and $member_count < 200)
        {
            $one_time_jfpoints = 9;
        }
        elseif($member_count >= 200 and $member_count < 300)
        {
            $one_time_jfpoints = 11;
        }
        elseif($member_count >= 300)
        {
            $n = floor($member_count/100);
            $one_time_jfpoints = 15 * $n;
        }
        if($one_time_jfpoints)
        {
            //判断是否加过积分 避免出错后 重复执行 重复添加积分
            $is_got = $this->newjfpointsm->get_today_add_count( $gauid, $type,"day");
            if(!$is_got)
            {
                $result = $this->newjfpointsm->add_jfpoints_out_config($gauid,$stage,$desc,$one_time_jfpoints,$type,$related_id=0,$related_type=0);
                //如果成功的话 需要发消息
                if($result['code'] == 0)
                {
                    $send_content = array("content"=>$stage);
                    $send_content = json_encode($send_content);
                    $message_result = $this->messagem->send_msg_basic(31495, $gauid, $send_content,$apptype=2,$msgtype=3);//默认用轻盈医学官方账号发送
                    w_log(json_encode($message_result));
                }
                return $result;
            }
            else
            {
                return array("code"=>-11,"message"=>"积分已经加过");
            }
        }
        else
        {
            return array("code"=>-10,"message"=>"成员低于最低数50  不给加积分");
        }
    }
}